package cn.psh.system.service.impl;

import cn.psh.system.domain.Role;
import cn.psh.system.mapper.RoleMapper;
import cn.psh.system.service.IRoleService;
import cn.psh.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author pider
 * @since 2022-09-21
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Transactional
    @Override
    public void del(Long id) {
        roleMapper.delRolePermissionByRoleId(id);
        roleMapper.delRoleMenuByRoleId(id);
        //删除角色
        roleMapper.del(id);
    }

    @Override
    public void save(Role role) {
        roleMapper.save(role);

        if(role.getPermission()!=null && role.getPermission().size()>0){
            roleMapper.saveRolePermission();
        }
        if(role.getMenus()!=null && role.getMenus().size()>0){
            roleMapper.saveRoleMeuns();
        }
//
    }

    @Override
    public void batchDel(List<Long> ids) {
        for (Long id : ids) {
            del(id);
        }
    }

    @Override
    public void update(Role role) {
        super.update(role);
        roleMapper.delRolePermissionByRoleId(role.getId());
        roleMapper.delRoleMenuByRoleId(role.getId());

        if(role.getPermission()!=null && role.getPermission().size()>0){
            roleMapper.saveRolePermission();
        }
        if(role.getMenus()!=null && role.getMenus().size()>0){
            roleMapper.saveRoleMeuns();
        }
    }
}
